import csv
import json
import json
import matplotlib.colors as mcolors
import math
import matplotlib.pyplot as plt
import random
import fun as fun
import numpy as np
import pandas as pd

colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())

# plt.figure(figsize=(40, 40), dpi=60)
# ax = plt.subplot(111)

# ax.xaxis.grid(True, which='major') #x坐标轴的网格使用主刻度
# ax.yaxis.grid(True, which='major') #x坐标轴的网格使用主刻度


def view_map(index):
    with open("./data/RoadLine.json", 'rb') as load_f:
        RoadLine = json.load(load_f)
    dt = RoadLine[index]["geometry"]["coordinates"]
    if dt == []:
        return
    dtarray = np.array(dt)
    plt.plot(dtarray[:, 1], dtarray[:, 0], color=colors[0], marker='.')

    # with open("./data/RoadPoint.json", 'rb') as load_f:
    #     RoadPoint = json.load(load_f)

    # for rd in RoadPoint:  # len(RoadLine)):
    #     dt = rd["geometry"]["coordinates"]
    #     if dt == []:
    #         continue
    #     dtarray = np.array(dt)
    #     plt.scatter(dtarray[1], dtarray[0],
    #                 color=colors[1], edgecolors='none', s=100)

def GET_my_path(points):
    path='C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\gen_people_25.csv' # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')
        
    _ldd = 0
    pltlins = []
    D5=[]
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)-1):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0], c='r', marker='o', s=700)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    c='b', marker='o', s=700)  # 起点
        _ldd += 1
        D5.append(d4)
    print(D5)
def view_my_path(points):
    path='C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\data\\gen_people_25.csv' # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')
        
    _ldd = 0
    pltlins = []
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)-1):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0], c='r', marker='o', s=700)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    c='b', marker='o', s=700)  # 起点
        _ldd += 1
        # print(d4)

        # plt.scatter(d4[:,1][0], d4[:,0][0],
        #             color=colors[7], edgecolors='none',marker='*')
if __name__ == '__main__':
    colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())

    points = fun.view_maps(True)
    print(len(points))
    view_my_path(points)
    plt.legend()
    plt.show()
